<?php
/*
	This SQL query will create the table to store your object.

	CREATE TABLE `crmuser` (
	`crmuserid` int(11) NOT NULL auto_increment,
	`contactid` int(11) NOT NULL,
	`credentialsid` int(11) NOT NULL,
	`login` VARCHAR(255) NOT NULL,
	`password` VARCHAR(255) NOT NULL, INDEX(`contactid`,`credentialsid`), PRIMARY KEY  (`crmuserid`)) ENGINE=MyISAM;
*/

/**
* <b>CRMUser</b> class with integrated CRUD methods.
* @author Php Object Generator
* @version POG 3.0f / PHP5.1 MYSQL
* @see http://www.phpobjectgenerator.com/plog/tutorials/45/pdo-mysql
* @copyright Free for personal & commercial use. (Offered under the BSD license)
* @link http://www.phpobjectgenerator.com/?language=php5.1&wrapper=pdo&pdoDriver=mysql&objectName=CRMUser&attributeList=array+%28%0A++0+%3D%3E+%27Contact%27%2C%0A++1+%3D%3E+%27Credentials%27%2C%0A++2+%3D%3E+%27login%27%2C%0A++3+%3D%3E+%27password%27%2C%0A++4+%3D%3E+%27SupportCase%27%2C%0A++5+%3D%3E+%27Activity%27%2C%0A++6+%3D%3E+%27Update%27%2C%0A++7+%3D%3E+%27Model%27%2C%0A%29&typeList=array%2B%2528%250A%2B%2B0%2B%253D%253E%2B%2527BELONGSTO%2527%252C%250A%2B%2B1%2B%253D%253E%2B%2527BELONGSTO%2527%252C%250A%2B%2B2%2B%253D%253E%2B%2527VARCHAR%2528255%2529%2527%252C%250A%2B%2B3%2B%253D%253E%2B%2527VARCHAR%2528255%2529%2527%252C%250A%2B%2B4%2B%253D%253E%2B%2527HASMANY%2527%252C%250A%2B%2B5%2B%253D%253E%2B%2527HASMANY%2527%252C%250A%2B%2B6%2B%253D%253E%2B%2527HASMANY%2527%252C%250A%2B%2B7%2B%253D%253E%2B%2527HASMANY%2527%252C%250A%2529
*/
include_once('class.pog_base.php');
class CRMUser extends POG_Base
{
	public $crmuserId = '';

	/**
	 * @var INT(11)
	 */
	public $contactId;
	
	/**
	 * @var INT(11)
	 */
	public $credentialsId;
	
	/**
	 * @var VARCHAR(255)
	 */
	public $login;
	
	/**
	 * @var VARCHAR(255)
	 */
	public $password;
	
	/**
	 * @var private array of SupportCase objects
	 */
	private $_supportcaseList = array();
	
	/**
	 * @var private array of Activity objects
	 */
	private $_activityList = array();
	
	/**
	 * @var private array of Update objects
	 */
	private $_updateList = array();
	
	/**
	 * @var private array of Model objects
	 */
	private $_modelList = array();
	
	public $pog_attribute_type = array(
		"crmuserId" => array('db_attributes' => array("NUMERIC", "INT")),
		"Contact" => array('db_attributes' => array("OBJECT", "BELONGSTO")),
		"Credentials" => array('db_attributes' => array("OBJECT", "BELONGSTO")),
		"login" => array('db_attributes' => array("TEXT", "VARCHAR", "255")),
		"password" => array('db_attributes' => array("TEXT", "VARCHAR", "255")),
		"SupportCase" => array('db_attributes' => array("OBJECT", "HASMANY")),
		"Activity" => array('db_attributes' => array("OBJECT", "HASMANY")),
		"Update" => array('db_attributes' => array("OBJECT", "HASMANY")),
		"Model" => array('db_attributes' => array("OBJECT", "HASMANY")),
		);
	public $pog_query;
	
	
	/**
	* Getter for some private attributes
	* @return mixed $attribute
	*/
	public function __get($attribute)
	{
		if (isset($this->{"_".$attribute}))
		{
			return $this->{"_".$attribute};
		}
		else
		{
			return false;
		}
	}
	
	function CRMUser($login='', $password='')
	{
		$this->login = $login;
		$this->password = $password;
		$this->_supportcaseList = array();
		$this->_activityList = array();
		$this->_updateList = array();
		$this->_modelList = array();
	}
	
	
	/**
	* Gets object from database
	* @param integer $crmuserId 
	* @return object $CRMUser
	*/
	function Get($crmuserId)
	{
		$connection = Database::Connect();
		$this->pog_query = "select * from `crmuser` where `crmuserid`='".intval($crmuserId)."' LIMIT 1";
		$cursor = Database::Reader($this->pog_query, $connection);
		while ($row = Database::Read($cursor))
		{
			$this->crmuserId = $row['crmuserid'];
			$this->contactId = $row['contactid'];
			$this->credentialsId = $row['credentialsid'];
			$this->login = $this->Unescape($row['login']);
			$this->password = $this->Unescape($row['password']);
		}
		return $this;
	}
	
	
	/**
	* Returns a sorted array of objects that match given conditions
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array $crmuserList
	*/
	function GetList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$connection = Database::Connect();
		$sqlLimit = ($limit != '' ? "LIMIT $limit" : '');
		$this->pog_query = "select * from `crmuser` ";
		$crmuserList = Array();
		if (sizeof($fcv_array) > 0)
		{
			$this->pog_query .= " where ";
			for ($i=0, $c=sizeof($fcv_array); $i<$c; $i++)
			{
				if (sizeof($fcv_array[$i]) == 1)
				{
					$this->pog_query .= " ".$fcv_array[$i][0]." ";
					continue;
				}
				else
				{
					if ($i > 0 && sizeof($fcv_array[$i-1]) != 1)
					{
						$this->pog_query .= " AND ";
					}
					if (isset($this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes']) && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'NUMERIC' && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'SET')
					{
						if ($GLOBALS['configuration']['db_encoding'] == 1)
						{
							$value = POG_Base::IsColumn($fcv_array[$i][2]) ? "BASE64_DECODE(".$fcv_array[$i][2].")" : "'".$fcv_array[$i][2]."'";
							$this->pog_query .= "BASE64_DECODE(`".$fcv_array[$i][0]."`) ".$fcv_array[$i][1]." ".$value;
						}
						else
						{
							$value =  POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$this->Escape($fcv_array[$i][2])."'";
							$this->pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
						}
					}
					else
					{
						$value = POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$fcv_array[$i][2]."'";
						$this->pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
					}
				}
			}
		}
		if ($sortBy != '')
		{
			if (isset($this->pog_attribute_type[$sortBy]['db_attributes']) && $this->pog_attribute_type[$sortBy]['db_attributes'][0] != 'NUMERIC' && $this->pog_attribute_type[$sortBy]['db_attributes'][0] != 'SET')
			{
				if ($GLOBALS['configuration']['db_encoding'] == 1)
				{
					$sortBy = "BASE64_DECODE($sortBy) ";
				}
				else
				{
					$sortBy = "$sortBy ";
				}
			}
			else
			{
				$sortBy = "$sortBy ";
			}
		}
		else
		{
			$sortBy = "crmuserid";
		}
		$this->pog_query .= " order by ".$sortBy." ".($ascending ? "asc" : "desc")." $sqlLimit";
		$thisObjectName = get_class($this);
		$cursor = Database::Reader($this->pog_query, $connection);
		while ($row = Database::Read($cursor))
		{
			$crmuser = new $thisObjectName();
			$crmuser->crmuserId = $row['crmuserid'];
			$crmuser->contactId = $row['contactid'];
			$crmuser->credentialsId = $row['credentialsid'];
			$crmuser->login = $this->Unescape($row['login']);
			$crmuser->password = $this->Unescape($row['password']);
			$crmuserList[] = $crmuser;
		}
		return $crmuserList;
	}
	
	
	/**
	* Saves the object to the database
	* @return integer $crmuserId
	*/
	function Save($deep = true)
	{
		$connection = Database::Connect();
		$this->pog_query = "select `crmuserid` from `crmuser` where `crmuserid`='".$this->crmuserId."' LIMIT 1";
		$rows = Database::Query($this->pog_query, $connection);
		if ($rows > 0)
		{
			$this->pog_query = "update `crmuser` set 
			`contactid`='".$this->contactId."', 
			`credentialsid`='".$this->credentialsId."', 
			`login`='".$this->Escape($this->login)."', 
			`password`='".$this->Escape($this->password)."'where `crmuserid`='".$this->crmuserId."'";
		}
		else
		{
			$this->pog_query = "insert into `crmuser` (`contactid`, `credentialsid`, `login`, `password`) values (
			'".$this->contactId."', 
			'".$this->credentialsId."', 
			'".$this->Escape($this->login)."', 
			'".$this->Escape($this->password)."')";
		}
		$insertId = Database::InsertOrUpdate($this->pog_query, $connection);
		if ($this->crmuserId == "")
		{
			$this->crmuserId = $insertId;
		}
		if ($deep)
		{
			foreach ($this->_supportcaseList as $supportcase)
			{
				$supportcase->crmuserId = $this->crmuserId;
				$supportcase->Save($deep);
			}
			foreach ($this->_activityList as $activity)
			{
				$activity->crmuserId = $this->crmuserId;
				$activity->Save($deep);
			}
			foreach ($this->_updateList as $update)
			{
				$update->crmuserId = $this->crmuserId;
				$update->Save($deep);
			}
			foreach ($this->_modelList as $model)
			{
				$model->crmuserId = $this->crmuserId;
				$model->Save($deep);
			}
		}
		return $this->crmuserId;
	}
	
	
	/**
	* Clones the object and saves it to the database
	* @return integer $crmuserId
	*/
	function SaveNew($deep = false)
	{
		$this->crmuserId = '';
		return $this->Save($deep);
	}
	
	
	/**
	* Deletes the object from the database
	* @return boolean
	*/
	function Delete($deep = false, $across = false)
	{
		if ($deep)
		{
			$supportcaseList = $this->GetSupportcaseList();
			foreach ($supportcaseList as $supportcase)
			{
				$supportcase->Delete($deep, $across);
			}
			$activityList = $this->GetActivityList();
			foreach ($activityList as $activity)
			{
				$activity->Delete($deep, $across);
			}
			$updateList = $this->GetUpdateList();
			foreach ($updateList as $update)
			{
				$update->Delete($deep, $across);
			}
			$modelList = $this->GetModelList();
			foreach ($modelList as $model)
			{
				$model->Delete($deep, $across);
			}
		}
		$connection = Database::Connect();
		$this->pog_query = "delete from `crmuser` where `crmuserid`='".$this->crmuserId."'";
		return Database::NonQuery($this->pog_query, $connection);
	}
	
	
	/**
	* Deletes a list of objects that match given conditions
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param bool $deep 
	* @return 
	*/
	function DeleteList($fcv_array, $deep = false, $across = false)
	{
		if (sizeof($fcv_array) > 0)
		{
			if ($deep || $across)
			{
				$objectList = $this->GetList($fcv_array);
				foreach ($objectList as $object)
				{
					$object->Delete($deep, $across);
				}
			}
			else
			{
				$connection = Database::Connect();
				$pog_query = "delete from `crmuser` where ";
				for ($i=0, $c=sizeof($fcv_array); $i<$c; $i++)
				{
					if (sizeof($fcv_array[$i]) == 1)
					{
						$pog_query .= " ".$fcv_array[$i][0]." ";
						continue;
					}
					else
					{
						if ($i > 0 && sizeof($fcv_array[$i-1]) !== 1)
						{
							$pog_query .= " AND ";
						}
						if (isset($this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes']) && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'NUMERIC' && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'SET')
						{
							$pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." '".$this->Escape($fcv_array[$i][2])."'";
						}
						else
						{
							$pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." '".$fcv_array[$i][2]."'";
						}
					}
				}
				return Database::NonQuery($pog_query, $connection);
			}
		}
	}
	
	
	/**
	* Associates the Contact object to this one
	* @return boolean
	*/
	function GetContact()
	{
		$contact = new Contact();
		return $contact->Get($this->contactId);
	}
	
	
	/**
	* Associates the Contact object to this one
	* @return 
	*/
	function SetContact(&$contact)
	{
		$this->contactId = $contact->contactId;
	}
	
	
	/**
	* Associates the Credentials object to this one
	* @return boolean
	*/
	function GetCredentials()
	{
		$credentials = new Credentials();
		return $credentials->Get($this->credentialsId);
	}
	
	
	/**
	* Associates the Credentials object to this one
	* @return 
	*/
	function SetCredentials(&$credentials)
	{
		$this->credentialsId = $credentials->credentialsId;
	}
	
	
	/**
	* Gets a list of SupportCase objects associated to this one
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array of SupportCase objects
	*/
	function GetSupportcaseList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$supportcase = new SupportCase();
		$fcv_array[] = array("crmuserId", "=", $this->crmuserId);
		$dbObjects = $supportcase->GetList($fcv_array, $sortBy, $ascending, $limit);
		return $dbObjects;
	}
	
	
	/**
	* Makes this the parent of all SupportCase objects in the SupportCase List array. Any existing SupportCase will become orphan(s)
	* @return null
	*/
	function SetSupportcaseList(&$list)
	{
		$this->_supportcaseList = array();
		$existingSupportcaseList = $this->GetSupportcaseList();
		foreach ($existingSupportcaseList as $supportcase)
		{
			$supportcase->crmuserId = '';
			$supportcase->Save(false);
		}
		$this->_supportcaseList = $list;
	}
	
	
	/**
	* Associates the SupportCase object to this one
	* @return 
	*/
	function AddSupportcase(&$supportcase)
	{
		$supportcase->crmuserId = $this->crmuserId;
		$found = false;
		foreach($this->_supportcaseList as $supportcase2)
		{
			if ($supportcase->supportcaseId > 0 && $supportcase->supportcaseId == $supportcase2->supportcaseId)
			{
				$found = true;
				break;
			}
		}
		if (!$found)
		{
			$this->_supportcaseList[] = $supportcase;
		}
	}
	
	
	/**
	* Gets a list of Activity objects associated to this one
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array of Activity objects
	*/
	function GetActivityList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$activity = new Activity();
		$fcv_array[] = array("crmuserId", "=", $this->crmuserId);
		$dbObjects = $activity->GetList($fcv_array, $sortBy, $ascending, $limit);
		return $dbObjects;
	}
	
	
	/**
	* Makes this the parent of all Activity objects in the Activity List array. Any existing Activity will become orphan(s)
	* @return null
	*/
	function SetActivityList(&$list)
	{
		$this->_activityList = array();
		$existingActivityList = $this->GetActivityList();
		foreach ($existingActivityList as $activity)
		{
			$activity->crmuserId = '';
			$activity->Save(false);
		}
		$this->_activityList = $list;
	}
	
	
	/**
	* Associates the Activity object to this one
	* @return 
	*/
	function AddActivity(&$activity)
	{
		$activity->crmuserId = $this->crmuserId;
		$found = false;
		foreach($this->_activityList as $activity2)
		{
			if ($activity->activityId > 0 && $activity->activityId == $activity2->activityId)
			{
				$found = true;
				break;
			}
		}
		if (!$found)
		{
			$this->_activityList[] = $activity;
		}
	}
	
	
	/**
	* Gets a list of Update objects associated to this one
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array of Update objects
	*/
	function GetUpdateList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$update = new Update();
		$fcv_array[] = array("crmuserId", "=", $this->crmuserId);
		$dbObjects = $update->GetList($fcv_array, $sortBy, $ascending, $limit);
		return $dbObjects;
	}
	
	
	/**
	* Makes this the parent of all Update objects in the Update List array. Any existing Update will become orphan(s)
	* @return null
	*/
	function SetUpdateList(&$list)
	{
		$this->_updateList = array();
		$existingUpdateList = $this->GetUpdateList();
		foreach ($existingUpdateList as $update)
		{
			$update->crmuserId = '';
			$update->Save(false);
		}
		$this->_updateList = $list;
	}
	
	
	/**
	* Associates the Update object to this one
	* @return 
	*/
	function AddUpdate(&$update)
	{
		$update->crmuserId = $this->crmuserId;
		$found = false;
		foreach($this->_updateList as $update2)
		{
			if ($update->updateId > 0 && $update->updateId == $update2->updateId)
			{
				$found = true;
				break;
			}
		}
		if (!$found)
		{
			$this->_updateList[] = $update;
		}
	}
	
	
	/**
	* Gets a list of Model objects associated to this one
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array of Model objects
	*/
	function GetModelList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$model = new Model();
		$fcv_array[] = array("crmuserId", "=", $this->crmuserId);
		$dbObjects = $model->GetList($fcv_array, $sortBy, $ascending, $limit);
		return $dbObjects;
	}
	
	
	/**
	* Makes this the parent of all Model objects in the Model List array. Any existing Model will become orphan(s)
	* @return null
	*/
	function SetModelList(&$list)
	{
		$this->_modelList = array();
		$existingModelList = $this->GetModelList();
		foreach ($existingModelList as $model)
		{
			$model->crmuserId = '';
			$model->Save(false);
		}
		$this->_modelList = $list;
	}
	
	
	/**
	* Associates the Model object to this one
	* @return 
	*/
	function AddModel(&$model)
	{
		$model->crmuserId = $this->crmuserId;
		$found = false;
		foreach($this->_modelList as $model2)
		{
			if ($model->modelId > 0 && $model->modelId == $model2->modelId)
			{
				$found = true;
				break;
			}
		}
		if (!$found)
		{
			$this->_modelList[] = $model;
		}
	}
}
?>